<!DOCTYPE html>
<html>
<head>
    <div th:include="base-libs-easyui :: base-libs-easyui" />
    <title>编辑机构执业信息</title>
    <style type="text/css">
        body {
            padding: 0px 10px;
        }
    </style>
</head>
<script th:inline="javascript">
    /**读取request中的初始化数据*/
    var requestData = [[${initData}]];
</script>
<script type="text/javascript">
    $(function() {
//        console.log(parent);
        initView();
        initData();
    });

    function initView(){
        $("#tt").tabs({
            onSelect: function(title,index){
                var params = $("body").data("ddzyxx");
                if(!params){
                    if(index == 1){
                        var yljg = $("#dgYljg").datagrid("getSelected");
                        if(!yljg){
                            $("#tt").tabs("select", 0);
                            return;
                        }
                    }else if(index == 2){
                        var yljg = $("#dgYljg").datagrid("getSelected");
                        if(!yljg){
                            $("#tt").tabs("select", 0);
                            return;
                        }
                        $("#YLJGMC").val(yljg["YLJGMC"]);
                        var ksxx = $("#dgKsxx").datagrid("getSelected");
                        if(ksxx){
                            $("#KSMC").val(ksxx["KSMC"]);
                        }
                        else{
                            $("#KSMC").val("");
                        }
                        return;
                    }
                }
            }
        });
        $("#dgYljg").datagrid({
            idField : "PTJGID",
            fitColumns : true,
            fit : true,
            border : false,
            rownumbers: true,
            singleSelect: true,
            toolbar : "#dgYljgToolbar",
            pagination: true
            ,url: contextRoot + "/yljgxx/queryPageYljgxx.service"
            ,columns:[[
                {
                    field:'YLJGMC'
                    ,title:'机构名称'
                    ,width:240
                }
                ,{
                    field:'XZQHMC'
                    ,title:'行政区划'
                    ,width:80
                }
                ,{
                    field:'WSJGLBMC'
                    ,title:'机构类别'
                    ,width:80
                }
                ,{
                    field:'YYDJMC'
                    ,title:'机构等级'
                    ,width:80
                }
                ,{
                    field:'CZ'
                    ,title:'操作'
                    ,width:40
                    ,formatter : function(value, row, index){
                        var tableToolBar = $("#tableToolBar").html();
                        return tableToolBar.replace(/targetId/g, row["PTJGID"]).replace(/targetAction/g, "selectYljg").replace(/targetDgId/g, "dgYljg");
                    }
                }
            ]]
            ,onBeforeLoad: function(queryParams) {
                var url = $(this).datagrid("options")["url"];
                /*URL不等空时再发送请求*/
                if (!StringUtils.isEmpty(url)) {
                    queryParams["pageSize"] = queryParams["rows"];
                    queryParams["pageNumber"] = queryParams["page"];
                    var YLJGMC = $("#YLJGMC_QUERY").val();
                    queryParams["YLJGMC"] = YLJGMC;
                    return true;
                }else{
                    return false;
                }
            }
            ,onSelect: function(index,row){
                $("#dgKsxx").datagrid("unselectAll");
                $("#dgKsxx").datagrid("load");
                $("#tt").tabs("select", 1);
            }
        });
        $("#dgKsxx").datagrid({
            idField : "KSDM",
            fitColumns : true,
            fit : true,
            border : false,
            rownumbers: true,
            singleSelect: true,
            pagination: true
            ,url: contextRoot + "/ksxx/.service"
            ,columns:[[
                {
                    field:'KSMC'
                    ,title:'科室名称'
                    ,width:240
                }
                ,{
                    field:'CZ'
                    ,title:'操作'
                    ,width:40
                    ,formatter : function(value, row, index){
                        var tableToolBar = $("#tableToolBar").html();
                        return tableToolBar.replace(/targetId/g, row["KSDM"]).replace(/targetAction/g, "selectKs").replace(/targetDgId/g, "dgKsxx");
                    }
                }
            ]]
            ,onBeforeLoad: function(queryParams) {
                var url = $(this).datagrid("options")["url"];
                /*URL不等空时再发送请求*/
                if (!StringUtils.isEmpty(url)) {
                    var yljg = $("#dgYljg").datagrid("getSelected");
                    if(!yljg){
                        return false;
                    }
                    queryParams["pageSize"] = queryParams["rows"];
                    queryParams["pageNumber"] = queryParams["page"];
                    queryParams["PTJGID"] = yljg["PTJGID"];
                    return true;
                }else{
                    return false;
                }
            }
            ,onSelect: function(index,row){
                $("#tt").tabs("select", 2);
            }
        })
        $("body").on("click", ".tableToolBarBtn", function(){
            var action = $(this).attr("action");
            var id = $(this).attr("id");
            var dgId = $(this).attr("targetDgId");
            $("#"+dgId).datagrid("selectRecord", id);
        })
        layui.use('laydate', function(){
            var laydate = layui.laydate;

            var start = {
                max: '2099-06-16 23:59:59'
                ,istoday: false
                ,choose: function(datas){
                    end.min = datas; //开始日选好后，重置结束日的最小日期
                    end.start = datas //将结束日的初始值设定为开始日
                }
            };

            var end = {
                min: laydate.now()
                ,max: '2099-06-16 23:59:59'
                ,istoday: false
                ,choose: function(datas){
                    start.max = datas; //结束日选好后，重置开始日的最大日期
                }
            };

            document.getElementById('QYKSRQ').onclick = function(){
                start.elem = this;
                laydate(start);
            }
            document.getElementById('QYJSRQ').onclick = function(){
                end.elem = this
                laydate(end);
            }
        });

        $("#btnOver").on("click", btnOverHandler)
    }

    /**点击完成回调*/
    function btnOverHandler(){
        var params = $("body").data("ddzyxx");
        if(!params){
            params = {};
        }
        var yljg = $("#dgYljg").datagrid("getSelected");
        /*新增*/
        if(!params.hasOwnProperty("trIndex")){
            if(!yljg){
                $.QuickAlert.alertFail({content: "请选择机构"});
            }else{
                params["PTJGID"] = yljg["PTJGID"];
                params["YLJGMC"] = yljg["YLJGMC"];
                var ksxx = $("#dgKsxx").datagrid("getSelected");
                if(ksxx){
                    params["KSDM"] = ksxx["KSDM"];
                    params["KSMC"] = ksxx["KSMC"];
                }else{
                    params["KSDM"] = "";
                    params["KSMC"] = "";
                }
                params["QYKSRQ"] = $("#QYKSRQ").val();
                params["QYJSRQ"] = $("#QYJSRQ").val();
                StoreCache.setCache("cache_ddzdxx", params);
                parent.layer.closeAll();
            }
        }else{
            params["QYKSRQ"] = $("#QYKSRQ").val();
            params["QYJSRQ"] = $("#QYJSRQ").val();
            StoreCache.setCache("cache_ddzdxx", params);
            parent.layer.closeAll();
        }
    }

    function initData(){
        var ddzyxx = StoreCache.removeCache("cache_ddzdxx");
        if(ddzyxx){
            $("body").data("ddzyxx", ddzyxx);
            $("#YLJGMC").val(ddzyxx["YLJGMC"]);
            $("#KSMC").val(ddzyxx["KSMC"]);
            $("#QYKSRQ").val(ddzyxx["QYKSRQ"]);
            $("#QYJSRQ").val(ddzyxx["QYJSRQ"]);
            $("#tt").tabs("select", 2);
            $("#tt").tabs("disableTab", 0);
            $("#tt").tabs("disableTab", 1);
        }
    }

    /**查询机构*/
    function btnQueryHandler(){
        $("#dgYljg").datagrid("load");
    }
</script>

<body class="easyui-layout">
    <div region="center" style="border: none;">
        <div id="tt" class="easyui-tabs" data-options="justified:true" fit="true">
            <div title="选择机构" style="padding:10px">
                <div id="dgYljgToolbar" style="padding:8px 20px;">
                    <table>
                        <tr>
                            <td class="query-text">医疗机构名称</td>
                            <td class="query-content">
                                <input id="YLJGMC_QUERY">
                            </td>
                            <td>
                                <button id="btnQuery" class="btn btn-default" type="button" onclick="btnQueryHandler()">查询</button>
                            </td>
                        </tr>
                    </table>
                </div>
                <table id="dgYljg"></table>
            </div>
            <div title="选择科室" style="padding:10px">
                <table id="dgKsxx"></table>
            </div>
            <div title="填写信息" style="padding:10px">
                <table class="form-table">
                    <tr>
                        <td class="td-text">
                            <label>选择机构</label>
                        </td>
                        <td class="td-content">
                            <input id="YLJGMC" readonly="readonly">
                        </td>
                    </tr>
                    <tr>
                        <td class="td-text">
                            <label>选择科室</label>
                        </td>
                        <td class="td-content">
                            <input id="KSMC" readonly="readonly">
                        </td>
                    </tr>
                    <tr>
                        <td class="td-text">
                            <label>签约开始日期</label>
                        </td>
                        <td class="td-content">
                            <input class="layui-input form-control"
                                   placeholder="签约开始日期" id="QYKSRQ" name="QYKSRQ"
                                   quick-config="label:'签约开始日期', validateProxy:'length', length: '10'">
                        </td>
                    </tr>
                    <tr>
                        <td class="td-text">
                            <label>签约结束日期</label>
                        </td>
                        <td class="td-content">
                            <input class="layui-input form-control"
                                   placeholder="签约结束日期" id="QYJSRQ" name="QYJSRQ"
                                   quick-config="label:'签约结束日期', validateProxy:'length', length: '10'">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <button id="btnOver" style="float: right; margin-right: 44px;">完成</button>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <script type="text/html" id="tableToolBar">
        <div>
            <a class="tableToolBarBtn" dgId=targetDgId action=targetAction id=targetId href="javascript:void(0)" title="选择" style="color: #00c297;">选择</a>
            <!--<a class="tableToolBarBtn" action="delete" id=PTJGID href="javascript:void(0)" title="删除" style="color: #00c297;">删除</a>-->
        </div>
    </script>
</body>
</html>


